package com.chengziting.cloud.zk;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;
import java.awt.*;

public class ZKTreeNode extends DefaultMutableTreeNode {
    private String key;
    private String data;
    private JLabel label;

    public ZKTreeNode(String key,String data){
        this.key = key;
        this.data = data;
        this.label = new JLabel(key);
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public Component getComponent(){
        return this.label;
    }

    public static class ZKTreeCellRenderer implements TreeCellRenderer{

        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            if(value instanceof ZKTreeNode){
                ZKTreeNode tn = (ZKTreeNode)value;
                Component com = tn.getComponent();
                if(selected){
                    com.setForeground(Color.BLUE);
                }else{
                    com.setForeground(Color.BLACK);
                }
                return com;
            }
            return new JLabel("");
        }
    }
}
